//
//  APIManager.m
//  xk100
//
//  Created by xiaokang on 2017/4/9.
//  Copyright © 2017年 xiaokang. All rights reserved.
//


#import "APIManager.h"
#import "XKMainNavigationController.h"
#import "XKLoginViewController.h"


@interface APIManager ()

//@property (nonatomic, strong) NSMutableDictionary *necessaryParametersDictM;// 必要参数

@end

@implementation APIManager

#pragma mark - 网络请求必要参数
NSDictionary *necessaryParametersDictM() {
    NSMutableDictionary * necessaryParametersDictM = [[NSMutableDictionary alloc] init];
    NSString *tmapStr  = [NSString getTimestamp];
    NSString *tempStr  = [NSString stringWithFormat:@"%@%@",APP_KEY_STRING,tmapStr];
    NSString *tokenStr = [tempStr md5String];
    NSNumber *time     = [NSNumber numberWithInteger:tmapStr.integerValue];
    NSNumber *uid      = [NSNumber numberWithInteger:[[UserDefaults objectWithKey:@"uid"] integerValue]];
    NSString *apptoken = [UserDefaults objectWithKey:@"apptoken"];
    
    [necessaryParametersDictM setObject:tokenStr forKey:@"token"];
    [necessaryParametersDictM setObject:time forKey:@"time"];
    [necessaryParametersDictM setObject:uid forKey:@"uid"];
    [necessaryParametersDictM setObject:apptoken forKey:@"apptoken"];
    return necessaryParametersDictM.copy;
}

#pragma mark - 发起网络请求
+ (void)POSTRequestWith:(APIPOSTRequestState)requestState parameters:(NSDictionary *)parameters successBlock:(void (^)(NSDictionary *))successBlock failureBlock:(void (^)(NSDictionary *))failureBlock errorBlock:(void (^)(NSError *))errorBlock {
    
    
    // 该BOOL值用于同一个账户在不同客户端登录后 来判断是否进行网络请求操作(登录页面默认设置为可操作,如果网络请求发现appToken失效就说明账户在另一个客户端登录,此时当前客户端不再允许网络请求操作)
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"isCanOperation"]) {
        return;
    }
    
    NSMutableDictionary *dictM = [NSMutableDictionary dictionaryWithDictionary:necessaryParametersDictM()];
    [dictM addEntriesFromDictionary:parameters];
    NSString *sub_url = nil;
    
    switch (requestState) {
        case 100000:{
            sub_url = GetVCode_Url;
        } break;
        case 100001:{
            sub_url = RegisterID_Url;
        } break;
        case 100002:{
            sub_url = ForgetPwd_Url;
        } break;
        case 100003:{
            sub_url = UpdateLoginPwd_Url;
        } break;
        case 100004:{
            sub_url = UpdateTradePwd_Url;
        } break;
        case 100005:{
            sub_url = Login_Url;
        } break;
        case 100006:{
            sub_url = WOSNewPrice_Url;
        } break;
        case 100007:{
            sub_url = News_Url;
        } break;
        case 100008:{
            sub_url = Notices_Url;
        } break;
        case 100009:{
            sub_url = Withdraw_Url;
        } break;
        case 100010:{
            sub_url = BuyWOS_Url;
        } break;
        case 100011:{
            sub_url = SellWOS_Url;
        } break;
        case 100012:{
            sub_url = UserDetails_Url;
        } break;
        case 100013:{
            sub_url = MarketDetails_Url;
        } break;
        case 100014:{
            sub_url = GetAuthState_Url;
        } break;
        case 100015:{
            sub_url = Recharge_Url;
        } break;
        case 100016:{
            sub_url = RevokeCommission_Url;
        } break;
        case 100017:{
            sub_url = BankList_Url;
        } break;
        case 100018:{
            sub_url = BindCard_Url;
        } break;
        case 100019:{
            sub_url = LevelUP_Url;
        } break;
        case 100020:{
            sub_url = UserAuth_Url;
        } break;
        case 100021:{
            sub_url = BankCardDelete_Url;
        } break;
        case 100022:{
            sub_url = CheckAppVersion_Url;
        } break;
        default:
            break;
    }
    
    NSString *post_url = [NSString stringWithFormat:@"%@%@",base_url,sub_url];
    
    [PPNetworkHelper POST:post_url parameters:dictM success:^(id responseObject) {
        if ([responseObject[@"flag"] isEqualToString:@"Success"]) {
            successBlock(responseObject);
        } else if ([responseObject[@"flag"] isEqualToString:@"false"]) {
            failureBlock(responseObject);
            NSString *msg = responseObject[@"msg"];
            if ([msg containsString:@"appToken已失效"]) {
                
                [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isCanOperation"];
                [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isLoginSuccess"];
                UIViewController *vc = [UIViewController currentViewController];
                [UIAlertController showAlertView:vc title:@"警告" message:@"系统检测到您的账号可能在另一客户端进行了登录,您已被迫下线!如果该操作不是您本人进行,请重新登录并修改登录密码和交易密码!" cancelButtonTitle:@"" confirmBtnTitle:@"确定" cancleBlock:^{
                    
                } confirmBlock:^{
                    UIWindow *window = [UIApplication sharedApplication].keyWindow;
                    XKMainNavigationController *nav = [[XKMainNavigationController alloc] initWithRootViewController:[XKLoginViewController new]];
                    window.rootViewController = nav;
                }];
                
                
            } else {
                [SVProgressHUD showErrorWithStatus:msg];
            }
        }
    } failure:^(NSError *error) {
        errorBlock(error);
        if (!((requestState == 100013) || (requestState == 100006))) {
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        }
    }];
    
}


@end
